var frameWasRendered = false;

var searchParams = {
	stars: 0,
	cities: new Array(),
	citiesCount: 0,
	searchWord: ''
};

var options = {
	'width': 0,
	'height': 0
}

$(document).ready(function() {
	options['width']  = getWidthOfClientArea() - 200;
	options['height'] = getHeightOfClientArea() - 200;
	
	$('<div id="qtip-blanket">').css({
		position: 'absolute',
		top: $(document).scrollTop(), // Use document scrollTop so it's on-screen even if the window is scrolled
		left: 0,
		height: $(document).height(), // Span the full document height...
		width: '100%', // ...and full width

		opacity: 0.7, // Make it slightly transparent
		backgroundColor: 'black',
		zIndex: 5000  // Make sure the zIndex is below 6000 to keep it below tooltips!
	}).appendTo(document.body).hide();
	
	$('#stars').change(function(){
		searchParams.stars = parseInt($(this).val());
		filterHotels();
	});
	
	$('#anycity').click(function(){
		var checked = $(this).attr('checked');
		if (checked) {
			searchParams.cities = new Array();
			searchParams.citiesCount = 0;
			$(".searchscroll1 input[type='checkbox']").each(function(){
				$(this).attr('checked', false);
			});
		}
		
		filterHotels();
	});
	
	$(".searchscroll1 input[type='checkbox']").click(function() {
		var checked = $(this).attr('checked');
		var tmpVal  = parseInt($(this).val());
		
		if (checked) {
			searchParams.cities.push(tmpVal);
			searchParams.citiesCount++;
			$('#anycity').attr('checked', false);
		} else {
			searchParams.cities = $.map(searchParams.cities, function (a, i) {
				if (a == tmpVal) {
					searchParams.citiesCount--;
					return null;
				} else {
					return a;
				}
			});
			
			if (searchParams.citiesCount == 0) {
				$('#anycity').attr('checked', true);
			}
		}
		
		filterHotels();
	});
	
	$('#anyhotel').click(function() {
		var checked = $(this).attr('checked');
		if (checked) {
			$(".searchscroll2 input[type='checkbox']").each(function(){
				$(this).attr('checked', false);
			});
			searchParams.searchWord = '';
			$('#hotelsearch').val('');
		}
		
		filterHotels();
	});
	
	$(".searchscroll2 input[type='checkbox']").click(function() {
		var checked = $(this).attr('checked');
		if (checked) {
			$("#anyhotel").attr('checked', false);
		}
	});
	
	$('#hotelsearch').keypress(function() {
		$("#anyhotel").attr('checked', false);
	});
	
	$('#hotelsearch').keyup(function() {
		searchParams.searchWord = $('#hotelsearch').val();
		filterHotels();
	});
	
	function filterHotels()
	{
		$(".searchscroll2 input[type='checkbox']").each(function() {
			var stars   = parseInt($(this).attr('stars'));
			var cityId  = parseInt($(this).attr('cityId'));
			var title   = $(this).next().html();
			var checked = $(this).attr('checked');
			if (searchParams.stars == 0 || (stars > 0 && stars == searchParams.stars)) {
				if (searchParams.citiesCount == 0 || (searchParams.cities.lenght != 0 && $.inArray(cityId, searchParams.cities) != -1)) {
					if (searchParams.searchWord == '' || (searchParams.searchWord != '' && title.match(new RegExp ('^' + searchParams.searchWord + '[\\w\\s]*', 'i')))) {
						$(this).parent().show();
					} else {
						$(this).parent().hide();
					}
				} else {
					$(this).parent().hide();
				}
			} else {
				$(this).parent().hide();
			}
		});
	}
	
	$('#formSearch').click(function() {
		showSuccessWindow();
	});
});

function showSuccessWindow() {
	$('#qtip-blanket').qtip({
			content: {
				title: {
					text: '<span class="popupTxt">Результаты поиска</span>',
					button: 'Закрыть'
				},
				text: '<div class="findContent" style="height: ' + options['height'] + 'px;"></div>'
			},
			position: {
				target: $(document.body), // Position it via the document body...
				corner: 'center' // ...at the center of the viewport
			},
		    show: {
				when: false, // Don't specify a show event
				ready: true // Show the tooltip when ready
		    },
			hide: false,
			style: {
				width: { max: options['width'], min: options['width'] },
				padding: '14px',
				border: {
					width: 9,
					radius: 9,
					color: '#666666'
				},
				name: 'light'
			},
			api: {
				beforeShow: function()
				{
					// Fade in the modal "blanket" using the defined show speed
					$('#qtip-blanket').fadeIn(this.options.show.effect.length);
				},
				beforeHide: function()
				{
					// Fade out the modal "blanket" using the defined hide speed
		            $('#qtip-blanket').fadeOut(this.options.hide.effect.length);
				},
				onShow: function()
				{
					var searchFormParams = $('#searchForm').formSerialize();
					var iframeUrl = '/hotel/iframesearch/?gridWidth=' + options['width'] + '&gridHeight=' + options['height'] + '&' + searchFormParams;
					
					if (!frameWasRendered) {
						$('.findContent').html('<iframe name="searchGrid" style="overflow:auto; width:100%; height:99%;" frameborder="0" src="' + iframeUrl + '"></iframe>');
					} else {
						window.frames.searchGrid.location.href = iframeUrl;
					}
					
				}
			}
		});
}